<template>
  <div class="container" :style="'height: '+docheight+'px'">
    <leftcont></leftcont>
    <div class="righCont">
      <topcont></topcont>
      <div class="content">
        <div class="payTable">
          <div class="trendTop">轮播图列表({{ total }})</div>
          <div class="tableCont">
            <div class="tableSerchCont">
              
              <div class="headBtn">
                <btn value="新增" @click="addFn"></btn>
              </div>
            </div>
            <el-table
              :data="tableData"
              style="width: 100%">
              <el-table-column
                prop="id"
                label="id"
              >
              </el-table-column>
              <el-table-column
                label="轮播图片"
              >
              <template slot-scope="scope">
                <img style="height: 40px;" :src="scope.row.image+ossSrc" />
              </template>
              </el-table-column>
              <el-table-column
                prop="url"
                label="链接"
              >
              </el-table-column>
              <el-table-column
                prop="sort"
                label="排序"
              >
              </el-table-column>
              <el-table-column
                  width="200"
                  label="操作">
                  <template slot-scope="scope">
                    <div class="ctrolBtn">
                        <div class="text_btn redBtn" @click="delFn(scope.row)">删除</div>
                        <div class="text_btn blueBtn" @click="editFn(scope.row)">编辑</div>
                    </div>
                  </template>
                </el-table-column>
            </el-table>
            <div class="pageCont">
              <el-pagination
                background
                layout="prev, pager, next"
                :page-size="per_page"
                :current-page="page"
                @current-change="pageFn"
                :total="total">
              </el-pagination>
            </div>
          </div>
        </div>
      </div>
    </div>
    <el-dialog
      :title="isAddBanner?'新增轮播图':'编辑轮播图'"
      :visible.sync="showAddBanner"
      width="600px"
      >
      <div class="addBanner">
        <div class="addFlexItem">
          <div class="flexItemName">跳转地址</div>
          <div class="flexItemVal">
            <el-input v-model="bannerInfo.url" style="width:400px" placeholder="请输入跳转地址"></el-input>
          </div>
        </div>
        <div class="addFlexItem">
          <div class="flexItemName">置顶层数</div>
          <div class="flexItemVal">
            <el-input v-model="bannerInfo.sort" style="width:400px" placeholder="请输入置顶层数"></el-input>
          </div>
        </div>
        <div class="addFlexItem">
          <div class="flexItemName">轮播图片</div>
          <div class="flexItemVal">
            <defUpImg :propImg="bannerInfo.image" width="120px" @upImgSuccess="getBannerImg"></defUpImg>
          </div>
        </div>
        <div class="addFlexItem">
          <div class="flexItemName"></div>
          <div class="flexItemVal">
            <btn v-if="isAddBanner" value="确认新增" @click="addBannerFn"></btn>
            <btn v-if="!isAddBanner" value="确认修改" @click="addBannerFn"></btn>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import leftcont from '@/components/leftCont.vue'
  import topcont from '@/components/topCont.vue'
  import btn from '@/components/public/btn.vue'
  import {Banner,BannerDel,bannerSave} from "@/http/api"
  import defUpImg from '@/components/public/defUpImg.vue'

  export default {
    components: {defUpImg,leftcont,topcont,btn},
    data() {
      return {
        tableData:[],
        page:1,
        per_page:5,
        total:0,
        showAddBanner:false,
        bannerInfo:{},
        isAddBanner:true,
      }
    },
    props: {},
    methods:{
      pageFn(index){
        this.page = index
        this.getdata()
      },
      getBannerImg(src){
        this.bannerInfo.image = src
      },
      addBannerFn(){
        let data = this.bannerInfo
        let loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.7)'
        });
        bannerSave(data).then((res) => {
          if(res.data.code==1){
            let msg = '新增成功'
            if(data.id){
              msg = '编辑成功'
            }
            this.$message({
              message: msg,
              type: 'success'
            });
            this.getdata()
          }else{
            this.$message(res.data.msg)
          }
          loading.close();
          this.showAddBanner = false
        }).catch(()=>{
          loading.close();
          this.$message('网络请求超时');
          this.showAddBanner = false
        })
      },
      showBannerMaskFn(type,item){
        this.isAddBanner = type
        if(type){
          this.bannerInfo = {
            sort:0,
            image:'',
            url:'',
          }
        }else{
          this.bannerInfo = {
            sort:item.sort,
            image:item.image,
            id:item.id,
            url:item.url,
          }
        }
        this.showAddBanner = true
      },
      addFn(){ 
        this.showBannerMaskFn(true,'')
      },
      editFn(item){
        this.showBannerMaskFn(false,item)
      },
      delFn(item){
        this.$confirm('确定删除此轮播图吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            let data ={
              id:item.id,
            }
            const loading = this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(255, 255, 255, 0.7)'
            });
            BannerDel(data).then((res)=>{
              if(res.data.code==1){
                this.$message({
                  type: 'success',
                  message: res.data.msg
                });
                this.getdata()
              }else{
                this.$message(res.data.msg)
              }
              loading.close()
            }).catch((err)=>{
              loading.close()
              this.$message('服务器请求超时');
            })
          })
      },
      getdata(){
        var data ={
          page:this.page,
          per_page:this.per_page,
        }
        Banner(data).then((res)=>{
          if(res.data.code==1){
            this.tableData = res.data.data.data //把最新的数组赋值给tableData
            this.total = res.data.data.total
          }else{
            this.$message(res.data.msg)
          }
        }).catch((err)=>{
          this.$message('服务器请求超时');
        })
      }
    },
    mounted(){
    },
    created() {
      this.getdata()
    },
  }
</script>

<style scoped lang="less">
  .container{
    width: 100%;
    display: flex;
    overflow: hidden;
    .righCont{
      display: flex;
      flex-direction: column;
      flex: 1;
      margin-left: 15px;
      height: 100%;
      width: 10px;
      background-color: #F4F8FF;
      .content{
        padding: 20px;
        box-sizing: border-box;
        width: 100%;
        flex: 1;
        overflow: auto;
        .payTable{
          margin-top: 20px;
          width: 100%;
          background-color: #fff;
          padding: 15px;
          box-sizing: border-box;
          .trendTop{
            font-weight: bold;
            font-size: 16px;
            cursor: pointer;
          }
          .tableCont{
            width: 100%;
            margin-top: 15px;
            .redBtn{
                color: #F56C6C;
              }
              .blueBtn{
                color: #96A1FE;
              }
              .greenBtn{
                color: #85CE61;
              }
              .blackBtn{
                color: #666;
              }
            .tableSerchCont{
              width: 100%;
              display: flex;
              justify-content: flex-end;
              margin-bottom: 10px;
              .serchItem{
                width: 150px;
                margin-left: 10px;
              }
              .headBtn{
                margin-left: 10px;
              }
            }
            .ctrolBtn{
              display: flex;
              .text_btn{
                margin: 0 5px;
                cursor: pointer;
                font-size: 14px;
              }
              

            }
            .pageCont{
              padding: 30px 0;
              display: flex;
              justify-content: center;
            }
          }
        }
      }
    }
    .addBanner{
      margin-top: 30px;
      .addFlexItem{
        width: 100%;
        display: flex;
        margin-bottom: 20px;
        .flexItemName{
          width: 100px;
          text-align: right;
          margin-right: 15px;
          font-size: 14px;
          line-height: 40px;
        }
        .flexItemVal{
          flex: 1;
          max-width: 400px;
        }
      }
    }
  }
</style>
